/*
 * Copyright (C) 2013 47 Degrees, LLC
 *  http://47deg.com
 *  hello@47deg.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.pricy.me;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.pricy.me.StreamProductsActivity.ListAppTask;
import com.pricy.me.adapters.ProductAdapter;
import com.pricy.me.model.ProductExtended;

public class MyProductsActivity extends ActionItems {

    private static final int REQUEST_CODE_SETTINGS = 0;
    private ProductAdapter adapter;
    private List<ProductExtended> data;

    private ListView swipeListView;

    private ProgressDialog progressDialog;
    private Handler noDataHandler ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Sherlock_Light);
        setContentView(R.layout.product_list_view);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        data = new ArrayList<ProductExtended>();

        adapter = new ProductAdapter(this, data);

        swipeListView = (ListView) findViewById(R.id.example_lv_list);

        
        SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(adapter);
        swingBottomInAnimationAdapter.setAbsListView(swipeListView);
        swipeListView.setAdapter(swingBottomInAnimationAdapter);

        reload();

        new ListAppTask().execute();

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.loading));
        progressDialog.setCancelable(false);
        progressDialog.show();
        
        noDataHandler =  new Handler(){
           	@Override
           	public void handleMessage(Message msg) {
           	}
           };


    }

    private void reload() {
//        swipeListView.setSwipeMode(SwipeListView.SWIPE_MODE_RIGHT);
//        swipeListView.setSwipeActionLeft(SwipeListView.SWIPE_ACTION_DISMISS);
//        swipeListView.setSwipeActionRight(SwipeListView.SWIPE_ACTION_REVEAL);
    }

    public int convertDpToPixel(float dp){
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float px = dp * (metrics.densityDpi/160f);
        return (int) px;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_SETTINGS:
                reload();
        }
    }

    public class ListAppTask extends AsyncTask<Void, Void, List<ProductExtended>> {

        protected List<ProductExtended> doInBackground(Void... args) {
        	ProductExtended product = new ProductExtended();
        	List<ProductExtended> products = product.getLocalProducts(getApplicationContext());
        	Collections.reverse(products);
            return products;
        }

        protected void onPostExecute(List<ProductExtended> result) {
        	if(result.size()==0){
          		MyProductsActivity.this.setContentView(R.layout.errordata);
            }
        	data.clear();
            data.addAll(result);
            adapter.notifyDataSetChanged();
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
        }
    }

	@Override
	void actualiser() {
		data = new ArrayList<ProductExtended>();
		progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.loading));
        progressDialog.setCancelable(false);
        progressDialog.show();
		new ListAppTask().execute();
		
	}

	@Override
	int whereami() {
		return MYPRODUCTS_ACTIVITY;
	}

}
